import { promiseTimeout } from './tools';

export function makePure<T>(obj: T): T {
	let prototype = Object.getPrototypeOf(obj);
	for (let keys = Object.keys(obj), i = 0; i < keys.length; i++) {
		let k = keys[i];
		let p = prototype[k];
		// if (p == undefined) {
		if (!obj.hasOwnProperty(k)) {
			// console.log('delete key', k);
			delete obj[k];
		}
	}
	return obj;
}

declare global {
	interface Object {
		pure<T>(): T;
	}

	interface Promise<T> {
		withTimeout(ms: number, tip?: string): Promise<T>;
	}
}

Object.defineProperty(Object.prototype, 'pure', {
	value: function<T>() {
		return makePure<T>(this);
	},
	enumerable: false,
});

Object.defineProperty(Promise.prototype, 'withTimeout', {
	value: function(ms, tip) {
		return promiseTimeout(ms, this, tip);
	},
	enumerable: false,
});
